package org.qydjk.console.controller.product;

import org.qydjk.common.controller.BaseController;
import org.qydjk.common.pojo.user.UserComment;
import org.qydjk.common.util.BeanUtil;
import org.qydjk.common.util.ListUtil;
import org.qydjk.common.util.StringUtil;
import org.qydjk.common.vo.EasyUIResponse;
import org.qydjk.common.vo.HibernatePage;
import org.qydjk.console.service.IProductService;
import org.qydjk.console.service.impl.ProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * Created by ASUS on 2017/12/26.
 */
@RequestMapping("/comment/")
@Controller
public class CommentController extends BaseController{

    @Autowired
    private IProductService productService;

    @RequestMapping("/comment.do")
    public String comment(){
       return "product/comment";
    }

    @RequestMapping("/commentDataGrid.do")
    @ResponseBody
    public Object commentDataGrid(int page, int rows){
        try {
            String findSql = "SELECT *  " +
                    "FROM qy_user_comment c";
            String countSql = "SELECT COUNT(c.id)   " +
                    "FROM qy_user_comment c";
            StringBuilder connectSql = new StringBuilder();
            List<Map<String, Object>> list = baseService.findMapBySQL(findSql,new HibernatePage(page,rows));
            int total = baseService.getRecordCountSQL(countSql);
            return new EasyUIResponse(total,list);

        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    @RequestMapping("/getImages.do")
    @ResponseBody
    public Object getImages(String id){
        try {
            String findImageSql = "SELECT i.img  " +
                    " FROM qy_user_comment_imgs i " +
                    " WHERE i.comment_id = "+id;
            List<Map<String, Object>> list = baseService.findMapBySQL(findImageSql);
            return list;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;

    }
    @RequestMapping("/getCommentContent.do")
    @ResponseBody
    public Object getCommentContent(String id){
        try {
            String findImageSql = "SELECT c.content,c.reply_content  " +
                    " FROM qy_user_comment c " +
                    " WHERE c.id = "+id;
            List<Map<String, Object>> list = baseService.findMapBySQL(findImageSql);
            return list;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;

    }

    @RequestMapping("/commentAddPage.do")
    public String commentAddPage(){
        return "product/commentAdd";
    }

    @RequestMapping("/add.do")
    @ResponseBody
    public Object add(UserComment userComment, String imgsUrl){

        try {
            Date date = new Date();
            userComment.setCreateTime(date);
            userComment.setUpdateTime(date);
            userComment.setReplyTime(date);
            BigDecimal sum = new BigDecimal(userComment.getOrganizationEnvironmentScore()).add(new BigDecimal(userComment.getOrganizationSeiviceScore())).add(new BigDecimal(userComment.getOrganizationSpeedScore()));
            Double star = Double.valueOf( sum.divide(new BigDecimal(3),1,BigDecimal.ROUND_HALF_UP).toString());
            userComment.setStar(star);

            productService.saveComment(userComment,imgsUrl);
            return renderSuccess("成功");
        } catch (Exception e) {
            e.printStackTrace();
            return  renderError("添加失败");
        }
    }

    @RequestMapping("commentEditPage")
    public String  commentEditPage(Model model,Integer id){
        try {
            UserComment userComment = baseService.get(UserComment.class, id);
            String findImgsSql = "SELECT i.img " +
                    " FROM qy_user_comment_imgs i " +
                    " WHERE i.comment_id = "+id;
            List<Map<String, Object>> imgsList = baseService.findMapBySQL(findImgsSql);
            String imgsString = "";
            if (!ListUtil.isNullOrEmpty(imgsList)){
                for (int i = 0 ;i < imgsList.size(); i++){
                    String imgUrl = String.valueOf(imgsList.get(i).get("img"));
                    imgsString += imgUrl+",";
                }
                model.addAttribute("imgsString",imgsString);
            }

            model.addAttribute("userComment",userComment);
        } catch (Exception e) {
            e.printStackTrace();
        }

        return "product/commentEdit";
    }

    @RequestMapping("/edit.do")
    @ResponseBody
    public Object edit(UserComment userComment,String imgsUrl){
        try {
            UserComment uc = baseService.get(UserComment.class, userComment.getId());

            Date date = new Date();
            uc.setUpdateTime(date);
            String rcNew = userComment.getReplyContent();
            String rcOld = uc.getReplyContent();

            if (!StringUtil.isEmptyOrNull(rcNew) && !StringUtil.isEmptyOrNull(rcOld) && !rcNew.equals(rcOld)){
                uc.setReplyTime(date);
            }
            BeanUtil.copyProperties(userComment,uc);
            productService.updateComment(uc,imgsUrl);


            return  renderSuccess("修改成功");
        } catch (Exception e) {
            e.printStackTrace();
            return renderError("修改失败");
        }

    }

    @RequestMapping("/delete.do")
    @ResponseBody
    public Object delete(UserComment userComment){

        try {
            productService.deleteComment(userComment);
            return renderSuccess("删除成功");
        } catch (Exception e) {
            e.printStackTrace();
            return  renderError("删除失败");
        }
    }


}
